<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar @click-left="$router.go(-1)" left-arrow title="编辑资料">
    </van-nav-bar>
    <!-- 头像 -->
    <div class="avatar">
      <van-image fit="cover" round :src="userInfo.photo" />
      <!-- 文件上传 -->
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息 -->
    <van-cell-group>
      <van-cell
        is-link
        title="名称"
        :value="userInfo.name"
        @click="showDialogClick"
      />
      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        @click="showPopup = true"
      />
      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        ref="nameFiled1"
        @click="showDatePick = true"
      />
    </van-cell-group>
    <!-- 编辑姓名弹窗 -->
    <van-dialog
      v-model="showDialog"
      title="编辑名称"
      show-cancel-button
      @confirm="updateNickname"
    >
      <van-field ref="nameFiled" v-model="newName" placeholder="请输入昵称">
      </van-field>
    </van-dialog>
    <!-- 修改性别 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        left-arrow
        @click-left="showPopup = false"
      >
      </van-nav-bar>
      <van-cell is-link title="男" @click="updateGender(0)" />
      <van-cell title="女" is-link @click="updateGender(1)" />
    </van-popup>
    <!-- 修改生日 -->
    <van-popup v-model="showDatePick" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDatePick = false"
        @confirm="hotBirthday"
      />
    </van-popup>
    <!-- 裁切盒子 -->
    <!-- 才窃取弹框 -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        fixed
        autoCropWidth="120px"
        autoCropHeight="120px"
        autoCrop
        :img="img"
      ></vueCropper>
      <!-- 按钮 -->
      <van-button class="que-btn" @click="suphoto" type="primary"
        >确认</van-button
      >
      <van-button @click="showCropper = false" class="quxiao-btn" type="info"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
// 导入mapState辅助函数
import { mapState } from 'vuex'
// 导入接口
import { editUserInfo, editUserAvatar } from './user.js'
// 导入moment
import moment from 'moment'
// 导入vue-cropper
import { VueCropper } from 'vue-cropper'

export default {
  data () {
    return {
      showDialog: false, //编辑姓名弹窗
      newName: '',
      showPopup: false, //编辑性别弹窗
      showDatePick: false, //生日弹窗
      minDate: new Date(1993, 3, 15),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      showCropper: false,
      img: ''
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  name: 'edit',
  components: { VueCropper },

  methods: {
    /*1.编辑姓名*/
    showDialogClick () {
      // 弹出dialog
      this.showDialog = true
      // 设置输入文本框
      this.newName = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.nameFiled.focus()
      })
    },
    /*2.修改名字 */
    updateNickname () {
      editUserInfo({ name: this.newName }).then(res => {
        //弹窗提示
        this.$toast.success('更改成功！')
        //更新vuex数据
        this.$store.commit('updateData', {
          ...this.userInfo, //先传入原始数据
          name: res.data.data.name //然后使用新的name覆盖原始的name
        })
      })
    },
    //3.修改性别
    updateGender (gender) {
      editUserInfo({ gender }).then(res => {
        //弹窗提示
        this.$toast.success('更改成功！')
        //更新vuex数据
        this.$store.commit('updateData', {
          ...this.userInfo, //先传入原始数据
          gender: res.data.data.gender == 1 ? 1 : 0 //然后使用新的name覆盖原始的name
        })
        //(3)关闭弹窗 popup弹窗默认是点击空白区域关闭。我们点击的是cell需要手动关闭
        this.showPopup = false
      })
    },
    //4. 修改生日
    hotBirthday (value) {
      // console.log(1)
      let birthday = moment(value).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        //弹窗提示
        this.$toast.success('更改成功！')
        //更新vuex数据
        this.$store.commit('updateData', {
          ...this.userInfo, //先传入原始数据
          birthday: res.data.data.birthday //覆盖
        })
        this.showDatePick = false //关闭弹窗
      })
    },
    // 5.上传图片
    afterRead (file) {
      // console.log(1)
      this.img = file.content
      this.showCropper = true
    },
    // 裁切确认按钮
    suphoto () {
      this.$refs.cropper.getCropBlob(blob => {
        let fd = new FormData()
        fd.append('photo', blob)
        editUserAvatar(fd).then(res => {
          this.$toast.success('更新成功')
          this.$store.commit('updateData', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          this.showCropper = false
        })
      })
    }
  }
}
</script>

<style lang="less">
.edit-container {
  // 导航条
  .van-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  // 头像
  .avatar {
    padding: 20px 0;
    text-align: center;
    .van-image {
      width: 120px;
      height: 120px;
    }
  }
  .van-uploader {
    position: absolute;
    width: 120px;
    height: 120px;
    left: 50%;
    opacity: 0;
    transform: translateX(-50%);
    .van-uploader__uploader {
      width: 120px;
      height: 120px;
    }
  }
  // 剪切框style
  .cropper-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .que-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .quxiao-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
